All files / core/storage DOMStorageWrapper.ts

90.48% Statements 19/21
75% Branches 3/4
85.71% Functions 6/7
90% Lines 18/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                16x                       16x   32x         32x           16x 19x     19x               16x 76x 76x 39x   37x             16x 16x                 16x 111x     16x     16x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { jsonEval, stringify } from '@firebase/util';
 
/**
 * Wraps a DOM Storage object and:
 * - automatically encode objects as JSON strings before storing them to allow us to store arbitrary types.
 * - prefixes names with "firebase:" to avoid collisions with app data.
 *
 * We automatically (see storage.js) create two such wrappers, one for sessionStorage,
 * and one for localStorage.
 *
 * @constructor
 */
export class DOMStorageWrapper {
  // Use a prefix to avoid collisions with other stuff saved by the app.
  private prefix_ = 'firebase:';
 
  /**
   * @param {Storage} domStorage_ The underlying storage object (e.g. localStorage or sessionStorage)
   */
  constructor(private domStorage_: Storage) {}
 
  /**
   * @param {string} key The key to save the value under
   * @param {?Object} value The value being stored, or null to remove the key.
   */
  set(key: string, value: any | null) {
    Iif (value == null) {
      this.domStorage_.removeItem(this.prefixedName_(key));
    } else {
      this.domStorage_.setItem(this.prefixedName_(key), stringify(value));
    }
  }
 
  /**
   * @param {string} key
   * @return {*} The value that was stored under this key, or null
   */
  get(key: string): any {
    const storedVal = this.domStorage_.getItem(this.prefixedName_(key));
    if (storedVal == null) {
      return null;
    } else {
      return jsonEval(storedVal);
    }
  }
 
  /**
   * @param {string} key
   */
  remove(key: string) {
    this.domStorage_.removeItem(this.prefixedName_(key));
  }
 
  isInMemoryStorage: boolean;
 
  /**
   * @param {string} name
   * @return {string}
   */
  prefixedName_(name: string): string {
    return this.prefix_ + name;
  }
 
  toString(): string {
    return this.domStorage_.toString();
  }
}